// pagesProperty/pages/asset/borrow/index.js
var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"),
formatTime = require("../../../../component/common.js");
import Toast from '../../../../component/vant/toast/toast';
import Dialog from '../../../../component/vant/dialog/dialog';

var common_Js = require("../../../../component/common.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetId:0,
    isLoading: true,
    is_estate_user:0,
    assetInfo:{},
    lastBorrow:{},
    submitState: false,
    from:'click'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('borrow', options);
    if (options.q) {
        var q = decodeURIComponent(options.q) // 获取到二维码原始链接内容
        q = "?" + q.split("?")[1];
        this.data.query = q;
        console.log('q', q)
        options = common_Js.getQueryVariable(q);
    }
    if(options.from){
      this.setData({
        from: options.from,
      })
    }
    if(options.id){
      this.data.assetId = options.id;
    }
    this.onHttpInit();
  },  
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/estate/asset/detail', {
      asset_id: that.data.assetId,
      op_type: 0,
    }, function (res) {
      console.log("获取资产信息", res)
      if (res.data.code == 0) {
        that.setData({
          hasData: true,
          is_estate_user: res.data.data.is_estate_user,
          assetInfo: res.data.data.asset,
          lastBorrow: res.data.data.borrowRecord,
        })
      }else{
        Toast.fail(res.data.msg)
      }
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({ netWork: true,loading: true,  isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false,loading: true,  isLoading: false, })
    })
  },
  goRecord(e){
    wx.navigateTo({
      url: './record/index?id=' + this.data.assetId,
    })
  },
  goBorrow(){
    var that = this;
    var message = "是否确认要借用该资产？"
    Dialog.confirm({
      title: '温馨提示',
      message,
      showConfirmButton:true,
      confirmButtonText:'确认借用',
    }).then(() => {      
      that.data.submitState = true;
      that.setData({submitState : true })
      that.onHttpSubmit();
    })
    .catch(() => {
      Dialog.close();
    });
  },
  
  onHttpSubmit: function () {
    var that = this;
    fetchApi.httpPost('/estate/asset/borrow', {
      asset_id: that.data.assetId,
    }, function (res) {
      console.log("获取资产信息", res)
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
        setTimeout(function(){
          wx.navigateBack({
            delta: -1,
          })
        }, 300);
      }else{
        Toast.fail(res.data.msg)
      }      
      that.data.submitState = false;
      that.setData({submitState : false })
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({ netWork: true,loading: true,  isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false,loading: true,  isLoading: false, })
    })
  },
})